/**
 * 检查角色坐骑数据脚本
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

async function checkCharacterMounts () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 查看角色2001的所有坐骑
    console.log('📊 角色2001的所有坐骑:');
    const [mountRows] = await connection.execute(`
            SELECT cm.*, mb.name as mountName 
            FROM character_mounts cm 
            JOIN mount_base mb ON cm.mountBaseId = mb.id 
            WHERE cm.characterId = 2001
            ORDER BY cm.id
        `);

    if (mountRows.length === 0) {
      console.log('❌ 角色2001没有任何坐骑');
      return;
    }

    console.log('ID\t坐骑名称\t等级\t转职\t当前攻击\t当前速度\t状态');
    console.log('--\t--------\t--\t--\t--------\t--------\t--');
    mountRows.forEach(row => {
      console.log(`${row.id}\t${row.mountName}\t\t${row.level}\t${row.transLevel}\t${row.currentAp}\t\t${row.currentSp}\t\t${row.status}`);
    });

    // 检查战马基础数据
    console.log('\n📊 战马基础数据:');
    const [baseRows] = await connection.execute('SELECT * FROM mount_base WHERE name = "战马"');
    if (baseRows.length > 0) {
      const baseData = baseRows[0];
      console.log(`ID: ${baseData.id}`);
      console.log(`名称: ${baseData.name}`);
      console.log(`初始攻击: ${baseData.apIni}`);
      console.log(`最大攻击: ${baseData.ap}`);
      console.log(`初始速度: ${baseData.spIni}`);
      console.log(`最大速度: ${baseData.sp}`);
    }

    // 检查是否有战马坐骑
    const zhanmaMount = mountRows.find(row => row.mountName === "战马");
    if (zhanmaMount) {
      console.log('\n🐎 找到战马坐骑，验证属性计算:');

      // 战马属性配置
      const zhanmaConfig = {
        ap: { initial: 2750, max: 2750 }, // 根据表格，战马未转时攻击不变
        speed: { initial: 50, max: 50 }   // 根据表格，战马未转时速度不变
      };

      const level = zhanmaMount.level;
      const transLevel = zhanmaMount.transLevel || 0;

      // 计算期望属性（未转状态）
      const expectedAp = Math.floor(zhanmaConfig.ap.initial + (zhanmaConfig.ap.max - zhanmaConfig.ap.initial) * Math.floor(level) / 100);
      const expectedSpeed = Math.floor(zhanmaConfig.speed.initial + (zhanmaConfig.speed.max - zhanmaConfig.speed.initial) * Math.floor(level) / 100);

      console.log(`等级: ${level}`);
      console.log(`转职等级: ${transLevel}`);
      console.log(`期望攻击: ${expectedAp}, 实际攻击: ${zhanmaMount.currentAp}`);
      console.log(`期望速度: ${expectedSpeed}, 实际速度: ${zhanmaMount.currentSp}`);

      const apMatch = expectedAp === zhanmaMount.currentAp;
      const speedMatch = expectedSpeed === zhanmaMount.currentSp;

      console.log(`攻击匹配: ${apMatch ? '✅' : '❌'}`);
      console.log(`速度匹配: ${speedMatch ? '✅' : '❌'}`);
    } else {
      console.log('\n❌ 角色2001没有战马坐骑');
    }

  } catch (error) {
    console.error('❌ 检查失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  checkCharacterMounts().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { checkCharacterMounts };
